{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f707a01d-fa77-4426-a589-d42433229c76",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.formula.api as smf\n",
    "from statsmodels.iolib.summary2 import summary_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "09359ed9-9df9-4eb6-a4d7-5e2879936797",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['RESPNO', 'COUNTRY', 'iso3n', 'DATEINTR', 'Q81', 'hascb', 'rgn', 'Q61',\n",
       "       'Q82B'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Set your file path here\n",
    "pathway=r\"Afrobarometer8_fordataverse.csv\"\n",
    "afb=pd.read_csv(pathway,index_col=0)\n",
    "afb.columns\n",
    "#Q81 = Ethnic group\n",
    "#Rgn = Region of Africa mentioned in Q61\n",
    "#hascb = Has cross-border coethnics (0/1) [coded by Lacina]\n",
    "#See below and Afrobarometer for additional variable definitions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b182e0d4-3676-49c4-990b-247b5af13fa4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "afb=afb.loc[((pd.notna(afb.Q61))&(afb.Q82B!='Not asked in the country'))].copy()\n",
    "len(afb.iso3n.unique().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "615ac0aa-c3be-4a3f-b877-d0046915ef01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2019-07-25'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "afb.DATEINTR.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "188ba7c7-bf2e-4e87-8f4f-ac435ebf5679",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-07-19'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "afb.DATEINTR.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fc3d0093-1aa7-4cbd-8e58-88b64f67d9df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q61 - Now let’s talk about how our country should relate to other countries in [region] Africa and the\n",
    "# world. Which of the following statements is closest to your view? Choose statement 1 or Statement 2.\n",
    "# Statement 1: People living in [region] Africa should be able to move freely across international borders in\n",
    "# order to trade or work in other countries. Statement 2: In order to protect their own citizens, governments should limit the cross-border movement of\n",
    "# people and goods\n",
    "\n",
    "conditions=[\n",
    "    (afb['Q61']=='Agree with 1')|(afb['Q61']=='Agree very strongly with 1'),\n",
    "    (afb['Q61']=='Agree with 2')|(afb['Q61']=='Agree very strongly with 2'),\n",
    "    (afb['Q61']=='Agree with neither')\n",
    "]\n",
    "choices=['open','closed','neutral']\n",
    "afb['borders']=np.select(conditions,choices,default=None)\n",
    "for x in choices:\n",
    "    afb[x]=(afb['borders']==x)*1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "785b49cf-b871-4c84-86d2-8cbc97611833",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q82B\n",
    "# Let us suppose that you had to choose between being a [nationality] and being a ________ [R’s Ethnic Group]. \n",
    "# Which of the following statements best expresses your feelings?\n",
    "\n",
    "conditions=[\n",
    "    (afb['Q82B']=='I feel equally (national identity) and (ethnic group)'),\n",
    "    (afb['Q82B']=='I feel only (national identity)')|(afb['Q82B']=='I feel more (national identity) than (ethnic group)'),\n",
    "    (afb['Q82B']=='I feel only (ethnic group)')|(afb['Q82B']=='I feel more (ethnic group) than (national identity)')\n",
    "]\n",
    "choices=['equal','ntnlty','ethnicity']\n",
    "afb['id']=np.select(conditions,choices,default=None)\n",
    "afb['id'] =afb['id'].astype('category').cat.set_categories(\n",
    "     new_categories = [\"ntnlty\",\"equal\",\"ethnicity\"], ordered = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "43e5b6e5-0825-4fd6-95f4-701cd87e69a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>id</th>\n",
       "      <th>ntnlty</th>\n",
       "      <th>equal</th>\n",
       "      <th>ethnicity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>32.000000</td>\n",
       "      <td>32.000000</td>\n",
       "      <td>32.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.405442</td>\n",
       "      <td>0.450653</td>\n",
       "      <td>0.143905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.168021</td>\n",
       "      <td>0.143441</td>\n",
       "      <td>0.063180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.135906</td>\n",
       "      <td>0.105777</td>\n",
       "      <td>0.043590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.298038</td>\n",
       "      <td>0.393254</td>\n",
       "      <td>0.092465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.391443</td>\n",
       "      <td>0.476818</td>\n",
       "      <td>0.148621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.497325</td>\n",
       "      <td>0.535378</td>\n",
       "      <td>0.177770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>0.801189</td>\n",
       "      <td>0.758865</td>\n",
       "      <td>0.303956</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "id        ntnlty      equal  ethnicity\n",
       "count  32.000000  32.000000  32.000000\n",
       "mean    0.405442   0.450653   0.143905\n",
       "std     0.168021   0.143441   0.063180\n",
       "min     0.135906   0.105777   0.043590\n",
       "25%     0.298038   0.393254   0.092465\n",
       "50%     0.391443   0.476818   0.148621\n",
       "75%     0.497325   0.535378   0.177770\n",
       "max     0.801189   0.758865   0.303956"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "afb.groupby('iso3n')['id'].value_counts(normalize=True).reset_index().pivot(index='iso3n',columns='id',values='proportion').describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "563b2988-e264-47c2-8ea4-8b75a487317f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>rgn</th>\n",
       "      <th>id</th>\n",
       "      <th>hascb</th>\n",
       "      <th>closed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Central</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.427119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Central</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.447087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Central</td>\n",
       "      <td>equal</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.426087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Central</td>\n",
       "      <td>equal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.500761</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Central</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.489051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Central</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.492188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.390779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.368252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>equal</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.371638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>equal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.377778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.347368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Eastern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.332859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Northern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.203252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Northern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.257885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Northern</td>\n",
       "      <td>equal</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.256098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Northern</td>\n",
       "      <td>equal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.249147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Northern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.468750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Northern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.263158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Southern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.310000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Southern</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.396546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>Southern</td>\n",
       "      <td>equal</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.315919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Southern</td>\n",
       "      <td>equal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.377830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Southern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.419162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Southern</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.413158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Western</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.403636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Western</td>\n",
       "      <td>ntnlty</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.355684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Western</td>\n",
       "      <td>equal</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.417352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Western</td>\n",
       "      <td>equal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.344380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Western</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.413793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Western</td>\n",
       "      <td>ethnicity</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.346460</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         rgn         id  hascb    closed\n",
       "0    Central     ntnlty    0.0  0.427119\n",
       "1    Central     ntnlty    1.0  0.447087\n",
       "2    Central      equal    0.0  0.426087\n",
       "3    Central      equal    1.0  0.500761\n",
       "4    Central  ethnicity    0.0  0.489051\n",
       "5    Central  ethnicity    1.0  0.492188\n",
       "6    Eastern     ntnlty    0.0  0.390779\n",
       "7    Eastern     ntnlty    1.0  0.368252\n",
       "8    Eastern      equal    0.0  0.371638\n",
       "9    Eastern      equal    1.0  0.377778\n",
       "10   Eastern  ethnicity    0.0  0.347368\n",
       "11   Eastern  ethnicity    1.0  0.332859\n",
       "12  Northern     ntnlty    0.0  0.203252\n",
       "13  Northern     ntnlty    1.0  0.257885\n",
       "14  Northern      equal    0.0  0.256098\n",
       "15  Northern      equal    1.0  0.249147\n",
       "16  Northern  ethnicity    0.0  0.468750\n",
       "17  Northern  ethnicity    1.0  0.263158\n",
       "18  Southern     ntnlty    0.0  0.310000\n",
       "19  Southern     ntnlty    1.0  0.396546\n",
       "20  Southern      equal    0.0  0.315919\n",
       "21  Southern      equal    1.0  0.377830\n",
       "22  Southern  ethnicity    0.0  0.419162\n",
       "23  Southern  ethnicity    1.0  0.413158\n",
       "24   Western     ntnlty    0.0  0.403636\n",
       "25   Western     ntnlty    1.0  0.355684\n",
       "26   Western      equal    0.0  0.417352\n",
       "27   Western      equal    1.0  0.344380\n",
       "28   Western  ethnicity    0.0  0.413793\n",
       "29   Western  ethnicity    1.0  0.346460"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Support for closed borders by region, feeling of closeness to ethnic group versus nation, and existence of cross-border coethnics\n",
    "cb=afb.groupby(['rgn','id','hascb'],observed=False)[['closed']].agg('mean').reset_index()\n",
    "cb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1d668cc2-7541-4f4b-9fd9-ca1d060683d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=================================================\n",
      "                             closed I  closed II \n",
      "-------------------------------------------------\n",
      "Intercept                   0.3147***  0.3096*** \n",
      "                            (0.0109)   (0.0118)  \n",
      "C(id)[T.equal]              0.0011     0.0036    \n",
      "                            (0.0046)   (0.0095)  \n",
      "C(id)[T.ethnicity]          0.0113*    0.0431*** \n",
      "                            (0.0064)   (0.0139)  \n",
      "C(COUNTRY)[T.Benin]         -0.0778*** -0.0787***\n",
      "                            (0.0152)   (0.0152)  \n",
      "C(COUNTRY)[T.Botswana]      0.3609***  0.3600*** \n",
      "                            (0.0182)   (0.0182)  \n",
      "C(COUNTRY)[T.Burkina Faso]  0.0138     0.0125    \n",
      "                            (0.0153)   (0.0153)  \n",
      "C(COUNTRY)[T.Cabo Verde]    0.1805***  0.1819*** \n",
      "                            (0.0203)   (0.0203)  \n",
      "C(COUNTRY)[T.Cameroon]      0.1070***  0.1054*** \n",
      "                            (0.0165)   (0.0166)  \n",
      "C(COUNTRY)[T.Côte d'Ivoire] 0.0799***  0.0790*** \n",
      "                            (0.0158)   (0.0158)  \n",
      "C(COUNTRY)[T.Eswatini]      0.1294     0.1282    \n",
      "                            (0.0860)   (0.0860)  \n",
      "C(COUNTRY)[T.Ethiopia]      0.0932***  0.0937*** \n",
      "                            (0.0140)   (0.0140)  \n",
      "C(COUNTRY)[T.Gabon]         0.1803***  0.1797*** \n",
      "                            (0.0173)   (0.0173)  \n",
      "C(COUNTRY)[T.Gambia]        -0.0299*   -0.0311*  \n",
      "                            (0.0168)   (0.0168)  \n",
      "C(COUNTRY)[T.Ghana]         0.1300***  0.1286*** \n",
      "                            (0.0132)   (0.0132)  \n",
      "C(COUNTRY)[T.Guinea]        0.0755***  0.0740*** \n",
      "                            (0.0146)   (0.0147)  \n",
      "C(COUNTRY)[T.Kenya]         0.0563***  0.0557*** \n",
      "                            (0.0138)   (0.0138)  \n",
      "C(COUNTRY)[T.Lesotho]       -0.0966*** -0.0974***\n",
      "                            (0.0184)   (0.0184)  \n",
      "C(COUNTRY)[T.Liberia]       -0.0731*** -0.0746***\n",
      "                            (0.0162)   (0.0162)  \n",
      "C(COUNTRY)[T.Malawi]        0.1118***  0.1113*** \n",
      "                            (0.0147)   (0.0147)  \n",
      "C(COUNTRY)[T.Mali]          0.0535***  0.0527*** \n",
      "                            (0.0155)   (0.0155)  \n",
      "C(COUNTRY)[T.Mauritius]     -0.0477*** -0.0488***\n",
      "                            (0.0176)   (0.0176)  \n",
      "C(COUNTRY)[T.Morocco]       -0.0643*** -0.0656***\n",
      "                            (0.0173)   (0.0174)  \n",
      "C(COUNTRY)[T.Mozambique]    0.1140***  0.1138*** \n",
      "                            (0.0161)   (0.0161)  \n",
      "C(COUNTRY)[T.Namibia]       0.1445***  0.1436*** \n",
      "                            (0.0167)   (0.0167)  \n",
      "C(COUNTRY)[T.Niger]         0.0546***  0.0534*** \n",
      "                            (0.0141)   (0.0142)  \n",
      "C(COUNTRY)[T.Nigeria]       0.0528***  0.0519*** \n",
      "                            (0.0144)   (0.0144)  \n",
      "C(COUNTRY)[T.Senegal]       -0.0212    -0.0227   \n",
      "                            (0.0155)   (0.0156)  \n",
      "C(COUNTRY)[T.Sierra Leone]  0.1635***  0.1627*** \n",
      "                            (0.0159)   (0.0159)  \n",
      "C(COUNTRY)[T.South Africa]  0.2015***  0.2003*** \n",
      "                            (0.0149)   (0.0149)  \n",
      "C(COUNTRY)[T.Tanzania]      0.0694***  0.0701*** \n",
      "                            (0.0140)   (0.0140)  \n",
      "C(COUNTRY)[T.Togo]          -0.0961*** -0.0971***\n",
      "                            (0.0172)   (0.0172)  \n",
      "C(COUNTRY)[T.Uganda]        -0.0758*** -0.0785***\n",
      "                            (0.0172)   (0.0172)  \n",
      "C(COUNTRY)[T.Zambia]        0.1089***  0.1079*** \n",
      "                            (0.0153)   (0.0153)  \n",
      "C(COUNTRY)[T.Zimbabwe]      -0.1484*** -0.1485***\n",
      "                            (0.0152)   (0.0152)  \n",
      "hascb                       0.0058     0.0133    \n",
      "                            (0.0061)   (0.0089)  \n",
      "C(id)[T.equal]:hascb                   -0.0030   \n",
      "                                       (0.0108)  \n",
      "C(id)[T.ethnicity]:hascb               -0.0403***\n",
      "                                       (0.0155)  \n",
      "R-squared                   0.0398     0.0399    \n",
      "R-squared Adj.              0.0392     0.0393    \n",
      "=================================================\n",
      "Standard errors in parentheses.\n",
      "* p<.1, ** p<.05, ***p<.01\n"
     ]
    }
   ],
   "source": [
    "# Regression model\n",
    "r0=smf.ols(formula=\"closed ~ C(id) + hascb + C(COUNTRY)\", data=afb).fit()\n",
    "r1=smf.ols(formula=\"closed ~ C(id)*hascb + C(COUNTRY)\", data=afb).fit()\n",
    "print(summary_col([r0,r1],stars=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b8a10510-bbdc-489a-a402-3c307ca7c8b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.043142</td>\n",
       "      <td>0.013855</td>\n",
       "      <td>3.113848</td>\n",
       "      <td>0.001848</td>\n",
       "      <td>0.015986</td>\n",
       "      <td>0.070297</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.043142  0.013855  3.113848  0.001848        0.015986         0.070297"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to ethnic group cf those who felt closest to nationality conditional on having no crossborder coethnics\n",
    "hyp='C(id)[T.ethnicity]=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b1f2eccf-0b43-4832-9cb0-7e6484efd61c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.002864</td>\n",
       "      <td>0.007211</td>\n",
       "      <td>0.397156</td>\n",
       "      <td>0.691254</td>\n",
       "      <td>-0.01127</td>\n",
       "      <td>0.016998</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.002864  0.007211  0.397156  0.691254        -0.01127         0.016998"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to ethnic group cf those who felt closest to nationality conditional on having crossborder coethnics\n",
    "hyp='C(id)[T.ethnicity]+C(id)[T.ethnicity]:hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b8250e84-012e-4454-a4cd-374cd2042c15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.039544</td>\n",
       "      <td>0.013314</td>\n",
       "      <td>2.970225</td>\n",
       "      <td>0.002977</td>\n",
       "      <td>0.01345</td>\n",
       "      <td>0.065639</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.039544  0.013314  2.970225  0.002977         0.01345         0.065639"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to ethnic group cf those who felt equally close to ethnicity and nationality conditional on having no crossborder coethnics\n",
    "hyp='C(id)[T.ethnicity]-C(id)[T.equal]=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b6dc2e56-7209-4ae7-bfe2-a39fe62c0f97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.002239</td>\n",
       "      <td>0.007023</td>\n",
       "      <td>0.318733</td>\n",
       "      <td>0.74993</td>\n",
       "      <td>-0.011527</td>\n",
       "      <td>0.016004</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t    P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.002239  0.007023  0.318733  0.74993       -0.011527         0.016004"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to ethnic group cf those who felt equally close to ethnicity and nationality conditional on having crossborder coethnics\n",
    "hyp='C(id)[T.ethnicity]+C(id)[T.ethnicity]:hascb-C(id)[T.equal]-C(id)[T.equal]:hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "12bdcea8-3e73-4c54-8175-6306a552075f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.003597</td>\n",
       "      <td>0.009485</td>\n",
       "      <td>0.379254</td>\n",
       "      <td>0.7045</td>\n",
       "      <td>-0.014994</td>\n",
       "      <td>0.022189</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t   P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.003597  0.009485  0.379254  0.7045       -0.014994         0.022189"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to nationality cf those who felt equally close to ethnicity and nationality conditional on having no crossborder coethnics\n",
    "hyp='C(id)[T.equal]=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4588faa6-1f72-4013-900c-630363cdf192",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.000625</td>\n",
       "      <td>0.005251</td>\n",
       "      <td>0.119095</td>\n",
       "      <td>0.905201</td>\n",
       "      <td>-0.009667</td>\n",
       "      <td>0.010918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.000625  0.005251  0.119095  0.905201       -0.009667         0.010918"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between those who felt closest to nationality cf those who felt equally close to ethnicity and nationality conditional on having crossborder coethnics\n",
    "hyp='C(id)[T.equal]+C(id)[T.equal]:hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f886e539-5562-4a12-b1bf-5f3811b0cd1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.013269</td>\n",
       "      <td>0.00894</td>\n",
       "      <td>1.484153</td>\n",
       "      <td>0.137774</td>\n",
       "      <td>-0.004254</td>\n",
       "      <td>0.030792</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef  std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.013269  0.00894  1.484153  0.137774       -0.004254         0.030792"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between respondents with and without crossborder coethnics conditional on feeling closest to one's nationality\n",
    "hyp='hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d108fab9-12c7-4c73-b461-c64287283409",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>-0.027009</td>\n",
       "      <td>0.013597</td>\n",
       "      <td>-1.986336</td>\n",
       "      <td>0.047001</td>\n",
       "      <td>-0.05366</td>\n",
       "      <td>-0.000358</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0 -0.027009  0.013597 -1.986336  0.047001        -0.05366        -0.000358"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between respondents with and without crossborder coethnics conditional on feeling closest to one's ethnicity\n",
    "hyp='hascb+C(id)[T.ethnicity]:hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1716ee8f-96a3-4923-a429-4c529a6adc28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>hyp</th>\n",
       "      <th>coef</th>\n",
       "      <th>std err</th>\n",
       "      <th>t</th>\n",
       "      <th>P&gt;|t|</th>\n",
       "      <th>Conf. Int. Low</th>\n",
       "      <th>Conf. Int. Upp.</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c0</td>\n",
       "      <td>0.010297</td>\n",
       "      <td>0.007869</td>\n",
       "      <td>1.308541</td>\n",
       "      <td>0.190695</td>\n",
       "      <td>-0.005126</td>\n",
       "      <td>0.02572</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  hyp      coef   std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.\n",
       "0  c0  0.010297  0.007869  1.308541  0.190695       -0.005126          0.02572"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Test for difference in means between respondents with and without crossborder coethnics conditional on feeling equally close to nationality and ethnicity\n",
    "hyp='hascb+C(id)[T.equal]:hascb=0'\n",
    "r1.t_test(hyp).summary_frame().reset_index(names=['hyp'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f393a963-1fee-4269-b2dd-de7aad14aaf3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d6952e7-6b15-42ef-b3da-adcb79b7758c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
